<?php
require('header.php');

if(isset($_POST['useraccount'])){
	$chosenId = intval($_POST['useraccount']);
	if(isset($_SESSION['cs_tmp_id']) && array_key_exists($chosenId, $_SESSION['cs_tmp_id'])){
		$userInfo = $_SESSION['cs_tmp_id'][$chosenId];

		$pageInfo = getPagesByUser($userInfo['user_id']);
		$groupInfo = getGroupsByUser($userInfo['user_id']);

		setUserLoginSession($userInfo);
		$_SESSION['cs_user_pages'] = $pageInfo;
		$_SESSION['cs_user_groups'] = $groupInfo;
		$_SESSION['cs_user_settings'] = $userInfo['user_setting_data'];
		if($userInfo['user_authorized'] == 'YES'){
			makeRedirect(route('auth'));
		}
		else{
			makeRedirect(route('index'));
		}
	}
	else{
		makeRedirect(route('login'));
	}
}
if(isset($_POST['username'])){
	$username = cleanValue($_POST['username']);
	$password = $_POST['password'];

	$userInfo = checkUserLogin($username, $password);
	if(!empty($userInfo)){
		$pageInfo = getPagesByUser($userInfo['user_id']);
		$groupInfo = getGroupsByUser($userInfo['user_id']);

		setUserLoginSession($userInfo);
		$_SESSION['cs_user_pages'] = $pageInfo;
		$_SESSION['cs_user_groups'] = $groupInfo;
		$_SESSION['cs_user_settings'] = $userInfo['user_setting_data'];
		if($userInfo['user_authorized'] == 'YES'){
			makeRedirect(route('auth'));
		}
		else{
			makeRedirect(route('index'));
		}
	}
}
?>
<div class="well">
	<form class="form-horizontal" role="form" method="post">
		<div class="form-group fg-username">
		    <label for="username" class="col-sm-4 control-label">Username or Email</label>
		    <div class="col-sm-4">
		      <input type="text" name="username" class="form-control" id="username" value="<?php echo isset($_POST['username'])?$_POST['username']:'' ?>">
		    </div>
		</div>
		<div class="form-group fg-password">
		    <label for="password" class="col-sm-4 control-label">Password</label>
		    <div class="col-sm-4">
		      <input type="password" name="password" class="form-control" id="password" value="">
		    </div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-4 col-sm-8">
			  <button type="submit" id="btn-save" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button>
			  <button type="button" id="btn-fblogin" class="btn btn-info" onClick="fbLogin()"><span class="glyphicon glyphicon-globe"></span> Login with Facebook</button>
			</div>
		</div>
		<hr>
		<div class="form-group">
			<div class="col-sm-offset-4 col-sm-8">
				<span> Dont' have an account? </span><a href="<?php echo route('register') ?>">Register now</a>
			</div>
		</div>

	</form>
	<!-- modal -->
	<form action="<?php echo route('login') ?>" method="post">
	<div class="modal fade" id="login-modal">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <h4 class="modal-title">Login option</h4>
	      </div>
	      <div class="modal-body">
	      	<div class="alert alert-info">
	      		<p>These following accounts was associated with this Facebook account.</p>
	      		<p>Please select one account to login with.</p>
	      	</div>
	        <ul class="list-group user-list">
			</ul>

	      </div>
	      <div class="modal-footer">
	        <button type="submit" class="btn btn-info">Login with selected account</button>
	      </div>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
	</form>
	<!--/ modal -->
</div>

<?php require('footer.php'); ?>